const express = require("express");
const multer = require("multer");
const path = require("path");
const { filePath } = require("../../config/application.config");
const router = express.Router();

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, filePath); // 设置文件存储路径
  },
  filename: function (req, file, cb) {
    const uniqueSuffix =
      Date.now() +
      "_" +
      Math.round(Math.random() * 1e9) +
      path.extname(file.originalname);
    cb(null, file.fieldname + "_" + uniqueSuffix); // 设置文件名
  },
});
const upload = multer({ storage });

router.post("/upload", upload.single("file"), (req, res, next) => {
  try {
    res.cc(`/file_assets/${req.file.filename}`, "上传成功");
  } catch (error) {
    next(error);
  }
});

module.exports = router;
